import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken } from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'
import Cookies from 'js-cookie'
import Vue from 'vue'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login', '/frame/report/call-list-person', '/frame/report/call-list-dept', '/frame/report/call-list-all', '/frame/report/index', '/frame/report/report-person', '/frame/report/report-dept'] // no redirect whitelist

router.beforeEach(async(to, from, next) => {
  // start progress bar
  NProgress.start()
  // set page title
  document.title = getPageTitle(to.meta.title)
  // determine whether the user has logged in
  const hasToken = getToken()

  // 设置头信息Authorization与sysCode
  try {
    // console.log('ready to set')
    const Authorization = to.query.Authorization || Cookies.get('Authorization')
    const sysCode = to.query.sysCode || Cookies.get('sysCode')

    if (Authorization && Authorization !== '') {
      // Cookies.set('Authorization', Authorization)
      localStorage.setItem('Authorization', Authorization)
    }
    if (sysCode && sysCode !== '' && sysCode !== 'undefined') {
      // Cookies.set('sysCode', sysCode)
      localStorage.setItem('sysCode', sysCode)
    }

    await Vue.prototype.loginUser()
  } catch (e) {
    console.log('set header exception:' + e)
  }

  // 获得参数传递
  const toName = to.name
  // 范围页面传参 all
  if (toName === 'call-list-all') {
    try {
      localStorage.setItem('queryRange', 'all')
    } catch (e) {
      console.log('set param exception:' + e)
    }
  } else if (toName === 'call-list-dept' || toName === 'report-dept') {
    try {
      localStorage.setItem('queryRange', 'dept')
    } catch (e) {
      console.log('set param exception:' + e)
    }
  } else if (toName === 'call-list-person' || toName === 'report-person') {
    try {
      localStorage.setItem('queryRange', 'person')
    } catch (e) {
      console.log('set param exception:' + e)
    }
  }

  if (hasToken) {
    if (to.path === '/login') {
      // if is logged in, redirect to the home page
      next({ path: '/' })
      NProgress.done()
    } else {
      const hasGetUserInfo = store.getters.name
      if (hasGetUserInfo) {
        next()
      } else {
        try {
          // get user info
          await store.dispatch('user/getInfo')

          next()
        } catch (error) {
          // remove token and go to login page to re-login
          await store.dispatch('user/resetToken')
          Message.error(error || 'Has Error')
          next(`/login?redirect=${to.path}`)
          NProgress.done()
        }
      }
    }
  } else {
    /* has no token*/

    if (whiteList.indexOf(to.path) !== -1) {
      // in the free login whitelist, go directly
      next()
    } else {
      // other pages that do not have permission to access are redirected to the login page.
      next(`/login?redirect=${to.path}`)
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
